/*
 * Register.java
 * 
 * Team Dovahkiin:
 * Major Bruno, Matthew Selfors
 * 
 * This class is a simulation of a single register from the SC-2.
 */

package phaseI;

/**
 * This class is a simulation of a single register from the SC-2 computer.
 * 
 * @author Major Bruno
 * @author Matthew Selfors
 * @version 1.0
 */
public class Register {
	
	/**
	 * The length of a byte.
	 */
	private static final int BYTE_LENGTH = 8;
	
	/**
	 * The length of a word.
	 */
	private static final int WORD_LENGTH = 16;

	/**
	 * The register value.
	 */
	private String register;

	/**
	 * Calls super() and initializes register with random value.
	 */
	public Register(final String value) {
		super();
		setRegValue(value);
	}

	/**
	 * Returns the low order byte of the register value.
	 * 
	 * @return the low order byte.
	 */
	public String getLowByte() {
		String result = register.substring(BYTE_LENGTH);
		
		return result;
	}

	/**
	 * Returns the high order byte of the register value.
	 * 
	 * @return the high order byte.
	 */
	public String getHighByte() {
		String result = register.substring(0, BYTE_LENGTH);
		
		return result;
	}

	/**
	 * Sets the low order byte of the register value.
	 * 
	 * @param value the low order byte.
	 */
	public void setLowByte(final String value) {
		String temp = value;
		for (int i = 0; i < BYTE_LENGTH - value.length(); i++) {
			temp = "0" + temp;
		}
		String hob = register.substring(0, BYTE_LENGTH);
		register = hob + temp;
	}

	/**
	 * Sets the high order byte of the register value.
	 * 
	 * @param value the high order byte.
	 */
	public void setHighByte(final String value) {
		String temp = value;
		for (int i = 0; i < BYTE_LENGTH - value.length(); i++) {
			temp = "0" + temp;
		}
		String lob = register.substring(BYTE_LENGTH);
		register = temp + lob;
	}

	/**
	 * Returns the sign (positive or negative) as a 0 or a 1, respectively.
	 * 
	 * @return 0 for positive, 1 for negative.
	 */
	protected int signOf() {
		int result;

		if (register.charAt(0) == '0') {
			result = 0;
		} else {
			result = 1;
		}

		return result;
	}

	/**
	 * Returns the value of the register.
	 * 
	 * @return the value.
	 */
	public String getRegValue() {
		return register;
	}

	/**
	 * Sets the register value.
	 * 
	 * @param value new value for register.
	 */
	public void setRegValue(final String value) {
		String temp = value;
		for (int i = 0; i < WORD_LENGTH - value.length(); i++) {
			temp = "0" + temp;
		}
		register = temp;
	}
}